package com.richard.library.basic.util;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * Description : 正则验证工具类
 * Author : admin-richard
 * Date : 2020-02-17 11:09
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2020-02-17 11:09     admin-richard         new file.
 * </pre>
 */
public final class RegexUtil {

    /**
     * 提取字符串中的数字
     *
     * @param input
     * @return
     */
    public static String getNumStr(CharSequence input) {
        return Pattern.compile("[^0-9]").matcher(input).replaceAll("");
    }

    /**
     * 提取指定字符串中字符
     *
     * @param content 提取内容
     * @param regex   指定提取字符正则表达式
     * @return 提取出的内容
     */
    public static String getStr(String content, String regex) {
        return Pattern.compile(regex).matcher(content).replaceAll("");
    }

    /**
     * 验证URL
     *
     * @param input 待验证文本
     * @return {@code true}: 匹配<br>{@code false}: 不匹配
     */
    public static boolean isURL(final CharSequence input) {
        return isMatch("[a-zA-z]+://[^\\s]*", input);
    }

    /**
     * 验证是否为汉字
     */
    public static boolean isChineseChar(String str) {
        return isMatch("[\u4e00-\u9fa5]", str);
    }

    /**
     * 验证是否全为字母
     */
    public static boolean isSpell(String str) {
        return isMatch("^[a-zA-Z]+$", str);
    }


    /**
     * 验证邮箱
     *
     * @param input 待验证文本
     * @return {@code true}: 匹配<br>{@code false}: 不匹配
     */
    public static boolean isEmail(CharSequence input) {
        return isMatch("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", input);
    }

    /**
     * 判断是否匹配正则
     *
     * @param regex 正则表达式
     * @param input 要匹配的字符串
     * @return {@code true}: 匹配<br>{@code false}: 不匹配
     */
    public static boolean isMatch(String regex, CharSequence input) {
//        return input != null && input.length() > 0 && Pattern.matches(regex, input);
        return Pattern.compile(regex).matcher(input).find();
    }


    /**
     * 去掉字符串中的空格
     *
     * @param targetStr 原字符串
     */
    public static String replaceSpace(String targetStr) {
        if (TextUtils.isEmpty(targetStr)) {
            return "";
        }
        return targetStr.replaceAll(" ", "");
    }

    /**
     * 利用正则表达式判断字符串是否是数字
     *
     * @param str 字符
     */
    public static boolean isNumeric(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern pattern = Pattern.compile("[0-9]*|-[0-9]*");
        Matcher isNum = pattern.matcher(str);
        return isNum.matches();
    }

    /**
     * 验证整数和浮点数（正负整数和正负浮点数）
     *
     * @param decimals 一位或多位0-9之间的浮点数，如：1.23，233.30
     * @return 验证成功返回true，验证失败返回false
     */
    public static boolean isDecimals(String decimals) {
        if (!TextUtils.isEmpty(decimals)) {
            Pattern decimalRegex = Pattern.compile("[0-9]\\d*|[0-9]\\d*.[0-9]\\d*|-[1-9]\\d*|-([0-9]\\d*.[0-9]\\d*)");
            return decimalRegex.matcher(decimals).matches();
        }
        return false;
    }

    /**
     * 手机号码验证
     *
     * @param mobiles 手机号
     */
    public static boolean isMobileNo(String mobiles) {
        if (TextUtils.isEmpty(mobiles)) {
            return false;
        }
        Pattern p = Pattern.compile("[1]\\d{10}");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /**
     * 验证是否属于IP V4 地址
     *
     * @param ip IP地址
     */
    public static boolean isIPV4(String ipv4) {
        if (!TextUtils.isEmpty(ipv4)) {
            return Pattern.compile("^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$")
                    .matcher(ipv4)
                    .matches();
        }
        return false;
    }

    /**
     * 验证是否属于IP V6 地址
     *
     * @param ipv6 IPV6地址
     */
    public static boolean isIPV6(String ipv6) {
        if (!TextUtils.isEmpty(ipv6)) {
            return Pattern.compile("^([\\da-fA-F]{1,4}:){6}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^::([\\da-fA-F]{1,4}:){0,4}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:):([\\da-fA-F]{1,4}:){0,3}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){2}:([\\da-fA-F]{1,4}:){0,2}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){3}:([\\da-fA-F]{1,4}:){0,1}((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){4}:((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$|^([\\da-fA-F]{1,4}:){7}[\\da-fA-F]{1,4}$|^:((:[\\da-fA-F]{1,4}){1,6}|:)$|^[\\da-fA-F]{1,4}:((:[\\da-fA-F]{1,4}){1,5}|:)$|^([\\da-fA-F]{1,4}:){2}((:[\\da-fA-F]{1,4}){1,4}|:)$|^([\\da-fA-F]{1,4}:){3}((:[\\da-fA-F]{1,4}){1,3}|:)$|^([\\da-fA-F]{1,4}:){4}((:[\\da-fA-F]{1,4}){1,2}|:)$|^([\\da-fA-F]{1,4}:){5}:([\\da-fA-F]{1,4})?$|^([\\da-fA-F]{1,4}:){6}:$")
                    .matcher(ipv6)
                    .matches();
        }
        return false;
    }

    /**
     * 检测是否为中文
     */
    public static boolean checkChina(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        Pattern p = Pattern.compile("[\\u4e00-\\u9fa5]{1,24}");
        Matcher m = p.matcher(str);
        return m.matches();
    }

    /**
     * 检查验证码是否合法
     *
     * @param verificationCode
     */
    public static boolean checkVerificationCode(String verificationCode) {
        if (TextUtils.isEmpty(verificationCode)) {
            return false;
        }
        Pattern p = Pattern.compile("[0-9]{6}$");
        Matcher m = p.matcher(verificationCode);
        return m.matches();
    }

    /**
     * 根据字符不同长度来“*”替换,达到保密
     *
     * @param str 需要“*”替换的字符串
     * @return “*”替换后的字符串
     */
    public static String replaceWithStar(String str) {
        if (str == null) {
            str = "";
        }

        String regular = "";
        int strLength = str.length();

        if (strLength <= 1) {
            regular = "\\*";
        } else if (strLength == 2) {
            regular = "(?<=\\w{0})\\w(?=\\w{1})";
        } else if (strLength <= 6) {
            regular = "(?<=\\d{1})\\d(?=\\d{1})";
        } else if (strLength == 7) {
            regular = "(?<=\\d{1})\\d(?=\\d{2})";
        } else if (strLength == 8) {
            regular = "(?<=\\d{2})\\d(?=\\d{2})";
        } else if (strLength == 9) {
            regular = "(?<=\\d{2})\\d(?=\\d{3})";
        } else if (strLength == 10) {
            regular = "(?<=\\d{3})\\d(?=\\d{3})";
        } else {
            regular = "(?<=\\d{3})\\d(?=\\d{4})";
        }

        return str.replaceAll(regular, "*");

    }
}
